package motodev.app;
import javax.microedition.lcdui.game.TiledLayer;

public class Board {

	/**
	 * Takes the game board and determines if a player has won.
	 * @param board The game board to check
	 * @param row The row number that the checker was placed in.
	 * @param col The column number that the checker was placed in.
	 * @return true if there is a winner false otherwise.
	 */
	public static boolean checkWinner(TiledLayer board, int row, int col)
	{
		int type = board.getCell(col, row);
		int numRows = board.getRows();
		int numCols = board.getColumns();
		int prevPrevCol = 0, prevCol = 0, nextCol = 0, nextNextCol = 0;
		int prevPrevRow = 0, prevRow = 0, nextRow = 0, nextNextRow = 0;
		int prevPrevDiagUp = 0, prevDiagUp = 0, nextDiagUp = 0, nextNextDiagUp = 0;
		int prevPrevDiagDown = 0, prevDiagDown = 0, nextDiagDown = 0, nextNextDiagDown = 0;
		
		// Check for horizontal 3
		if (col-1 >= 0)
			prevCol = board.getCell(col-1, row);
		if (col-2 >= 0)
			prevPrevCol = board.getCell(col-2, row);
		if (col+1 < numCols)
			nextCol = board.getCell(col+1, row);
		if (col+2 < numCols)
			nextNextCol = board.getCell(col+2, row);
		
		// Check [r,c-1] == [r,c]
		if (prevCol == type)
		{
			// Check [r,c-1] == [r,c] == [r,c+1]
			if (prevCol == nextCol)
			{
				// THREE IN A ROW
				return true;
			}
			
			// Check [r,c-2] == [r,c-1] == [r,c]
			if (prevCol == prevPrevCol)
			{
				// THREE IN A ROW
				return true;
			}
		}
		
		// Check [r,c] == [r,c+1]
		if (nextCol == type)
		{
			// Check [r,c] == [r,c+1] == [r,c+2]
			if (nextCol == nextNextCol)
			{
				// THREE IN A ROW
				return true;
			}
		}
		
		// Check for vertical 3
		if (row-1 >= 0)
			prevRow = board.getCell(col, row-1);
		if (row-2 >= 0)
			prevPrevRow = board.getCell(col, row-2);
		if (row+1 < numRows)
			nextRow = board.getCell(col, row+1);
		if (row+2 < numRows)
			nextNextRow = board.getCell(col, row+2);
		
		// Check [r-1,c] == [r,c]
		if (prevRow == type)
		{
			// Check [r-1,c] == [r,c] == [r+1,c]
			if (prevRow == nextRow)
			{
				// THREE IN A ROW
				return true;
			}
			
			// Check [r-2,c] == [r-1,c] == [r,c]
			if (prevRow == prevPrevRow)
			{
				// THREE IN A ROW
				return true;
			}
		}
		
		// Check [r,c] == [r+1,c]
		if (nextRow == type)
		{
			// Check [r,c] == [r+1,c] == [r+1,c]
			if (nextRow == nextNextRow)
			{
				// THREE IN A ROW
				return true;
			}
		}
		
		// Check for diagonal 3
		if (row+1 < numRows && col-1 >= 0)
			prevDiagDown = board.getCell(col-1, row+1);
		if (row+2 < numRows && col-2 >= 0)
			prevPrevDiagDown = board.getCell(col-2, row+2);
		if (row-1 >= 0 && col-1 >= 0)
			prevDiagUp = board.getCell(col-1, row-1);
		if (row-2 >= 0 && col-2 >= 0)
			prevPrevDiagUp = board.getCell(col-2, row-2);
		if (row+1 < numRows && col+1 < numCols)
			nextDiagDown = board.getCell(col+1, row+1);
		if (row+2 < numRows && col+2 < numRows)
			nextNextDiagDown = board.getCell(col+2, row+2);
		if (row-1 >= 0 && col+1 < numCols)
			nextDiagUp = board.getCell(col+1, row-1);
		if (row-2 >= 0 && col+2 < numRows)
			nextNextDiagUp = board.getCell(col+2, row-2);
		
		// Check [r-1,c-1] == [r,c]
		if (prevDiagDown == type)
		{
			// Check [r-1,c-1] == [r,c] == [r+1,c+1]
			if (prevDiagDown == nextDiagUp)
			{
				// THREE IN A ROW
				return true;
			}
			
			// Check [r-2,c-2] == [r-1,c-1] == [r,c]
			if (prevDiagDown == prevPrevDiagDown)
			{
				// THREE IN A ROW
				return true;
			}
		}
		
		// Check [r,c] == [r+1,c+1]
		if (nextDiagUp == type)
		{
			// Check [r,c] == [r+1,c+1] == [r+2,c+2]
			if (nextDiagUp == nextNextDiagUp)
			{
				// THREE IN A ROW
				return true;
			}
		}
		
		// Check [r-1,c-1] == [r,c]
		if (prevDiagUp == type)
		{
			// Check [r-1,c-1] == [r,c] == [r+1,c+1]
			if (prevDiagUp == nextDiagDown)
			{
				// THREE IN A ROW
				return true;
			}
			
			// Check [r-2,c-2] == [r-1,c-1] == [r,c]
			if (prevDiagUp == prevPrevDiagUp)
			{
				// THREE IN A ROW
				return true;
			}
		}
		
		// Check [r,c] == [r+1,c+1]
		if (nextDiagDown == type)
		{
			// Check [r,c] == [r+1,c+1] == [r+2,c+2]
			if (nextDiagDown == nextNextDiagDown)
			{
				// THREE IN A ROW
				return true;
			}
		}
		
		return false;
	}
}
